/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jpm.jroguefx.levels;

import com.jpm.jroguefx.Constants;
import com.jpm.jroguefx.types.TerrainType;
import javafx.scene.image.Image;

public class Tile implements Constants {

    private final TerrainType[] terrain = new TerrainType[5];
    private boolean isRiver = false;
    private boolean isRoad = false;
    private boolean isMountain = false;
    private boolean isBridge = false;

    public boolean isBridge() {
        return isBridge;
    }

    public void setBridge() {
        isBridge = true;
    }

    public void setMountain() {
        isMountain = true;
    }

    public boolean isMountain() {
        return isMountain;
    }

    public boolean isRiver() {
        return isRiver;
    }

    public void setRiver() {
        if (!isHouse()) {
            terrain[TOP] = null;
            isRiver = true;
        }
    }

    public boolean isRoad() {
        return isRoad;
    }

    public boolean isHouse() {
        return terrain[TOP] != null && terrain[TOP].isHouse();
    }

    public boolean isRoadPassable() {
        return !isMountain() && !isSea() && (terrain[RIVER] == null || (terrain[RIVER] == TerrainType.RIVERNS || terrain[RIVER] == TerrainType.RIVERWE));
    }

    public boolean isRiverPassable() {
        return !isMountain() && isSolidGround();
    }

    public boolean isMountainPassable() {
        return !isRiver() && !isRoad() && !hasTerrain(TOP);
    }

    public boolean isBridgePassable() {
        return isRoad() && !isBridge();
    }

    public void setRoad() {
        if (!isHouse()) {
            terrain[TOP] = null;
            isRoad = true;
        }
    }

    public boolean isTerrainType(TerrainType terrain, int level) {
        return this.terrain[level] != null && this.terrain[level].isSameTerrain(terrain);
    }

    public TerrainType getTerrain(int level) {
        return terrain[level];
    }

    public boolean hasTerrain(int level) {
        return terrain[level] != null;
    }

    public void setTerrain(TerrainType terrain, int level) {
        this.terrain[level] = terrain;
    }

    public boolean setTerrainType(TerrainType terrain, boolean overwrite, int level) {
        if ((isSolidGround() && this.terrain[level] == null) || overwrite) {
            if (terrain != null && (this.terrain[TOP] == null || !this.terrain[TOP].isHouse())) {
                this.terrain[level] = terrain;
                return true;
            }
        }
        return false;
    }


    public Image getTerrainImage(int level) {

        if (terrain[level] == null) {
            if (level == BASE) {
                terrain[level] = TerrainType.UNKNOWN;
                System.out.println("Missing l1Terrain Here!!!");
            } else {
                return null;
            }
        }
        return terrain[level].getImage();
    }


    public Image[] getTerrainImage() {
        return new Image[]{getTerrainImage(BASE), getTerrainImage(RIVER), getTerrainImage(ROAD)};
    }

    public Image[] getUpperImage() {
        return new Image[]{getTerrainImage(TOP), getTerrainImage(4)};
    }

    public boolean isSolidGround() {
        return !isRiver && !isSea();
    }

    public boolean isSea() {
        return terrain[BASE] != null && terrain[BASE].isSea();
    }

}
